/************************************************************************
Copyright 2003, 2004, 2005, 2006, 2007, 2008 Grid Technology Research Center,
National Institute of Advanced Industrial Science and Technology.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************/

package jp.aist.gtrc.plus.reserve;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.Permission;

import org.apache.log4j.Logger;

class ReserveSecurityManager extends SecurityManager {

	private final transient InetAddress localAddr;
	protected static final transient Logger logger = Logger.getLogger(ReserveSecurityManager.class);

	ReserveSecurityManager() throws ReserveException {

		try {
			localAddr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			throw new ReserveException(e.getMessage());
		}

	}

	public void checkAccept(String host, int port) throws SecurityException {

		/*
		 * NOTE: 'host' is String of "aaa.bbb.ccc.ddd" normally.
		 */
		logger.debug("accept from " + host + ":" + port);
		InetAddress clientAddr;
		try {
			clientAddr = InetAddress.getByName(host);
		} catch (UnknownHostException e) {
			throw new SecurityException("cannot accept from " + host + ":" + port);
		}

/*
 * Allow connection from remote host - Ekasit
 *
		if (!clientAddr.equals(localAddr) && !clientAddr.isLoopbackAddress()) {
			throw new SecurityException("cannot accept from " + host + ":" + port);
		}
*/

	}

	public void checkExec(String cmd) {

		/* all accesses are allowed, never throw SecurityException */

	}

	public void checkPermission(Permission perm) {

		checkPermission(perm, null);

	}

	public void checkPermission(Permission perm, Object context) {

		/* all accesses are allowed, never throw SecurityException */

	}

}
